package com.zn.loading
{
    import flash.events.Event;
    import flash.events.IOErrorEvent;
    import flash.events.ProgressEvent;
    import flash.net.URLLoader;

    public class XMLLoader extends LoaderItem
    {
        _activeTypes("XMLLoader", XMLLoader);

        private var _urlLoader:URLLoader;

        public function XMLLoader(url:String = null, name:String = null,noCache:Boolean=false)
        {
            super(url, name);
			
			this.noCache=noCache;
        }

        protected override function init():void
        {
            super.init();

            _urlLoader = new URLLoader();
            _urlLoader.addEventListener(Event.COMPLETE, completeHandler);
            _urlLoader.addEventListener(ProgressEvent.PROGRESS, progressHandler);
            _urlLoader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
        }

        protected override function completeHandler(event:Event):void
        {
            super.completeHandler(event);

            _content = _urlLoader.data;

            if (!_isParseLoaders)
            {
                parseLoader(XML(content));
            }
			
			loadComplete();
        }

        public override function load(flush:Boolean = false):void
        {
            if (loaderStatus != LoaderStatus.READY && !flush)
                return;

            super.load(flush);

            _urlLoader.load(_urlRequest);
        }

        protected function parseLoader(xml:XML, parentLoader:LoaderMax = null):void
        {
            _isParseLoaders = true;

            var loaderItem:LoaderItem;
            var curName:String;
            for each (var node:XML in xml.children())
            {
                curName = node.name();
                if (curName == "LoaderMax")
                {
                    loaderItem = new LoaderMax();
                    loaderItem.loaderXML = node;

                    parseLoader(node, loaderItem as LoaderMax);

                    if (parentLoader)
                        parentLoader.addChildLoad(loaderItem);

                }
                else if (curName in _types)
                {
                    var loaderClass:Class = _types[curName];
                    loaderItem = new loaderClass();
                    loaderItem.loaderXML = node;

                    if (parentLoader)
                        parentLoader.addChildLoad(loaderItem);
                }
            }
        }

        protected function loadComplete():void
        {
            loaderStatus = LoaderStatus.COMPLETED;
            dispatchEvent(new LoaderEvent(LoaderEvent.COMPLETE));
        }

        override public function unload():void
        {
			super.unload();
            if (_urlLoader)
            {
                LoaderMax.removeLoaderItem(name, false);
                try
                {
                    _urlLoader.close();
                }
                catch (e:Error)
                {

                }
				_urlLoader = null;
            }
        }
		
		override public function stop():void
		{
			super.stop();
			
			if (_urlLoader)
			{
				try
				{
					_urlLoader.close();
				} 
				catch(error:Error) 
				{
					
				}
			}
		}
		
		public override function pause():void
		{
			super.pause();
			
			if (_urlLoader)
			{
				try
				{
					_urlLoader.close();
				} 
				catch(error:Error) 
				{
					
				}
			}
		}

    }
}